package com.offer;


import java.util.Comparator;
import java.util.PriorityQueue;

public class Test42 {

    public int maxSubArray(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];

        int max = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (dp[i - 1] > 0) {
                dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
            } else {
                dp[i] = nums[i];
            }
            max = Math.max(dp[i], max);
        }

        return max;
    }

    public static void main(String[] args) {
        Test42 test = new Test42();
        System.out.println(test.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
    }

}
